package com.xigua._树;

import com.xigua.TreeNode;

/**
 * @author LiRongHua
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: 请实现一个函数，用来判断一棵二叉树是不是对称的。如果一棵二叉树和它的镜像一样，
 * 那么它是对称的。例如，二叉树 [1,2,2,3,4,4,3] 是对称的。
 *     1
   / \
  2   2
 / \ / \
3  4 4  3
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:

    1
   / \
  2   2
   \   \
   3    3

 * @date 2022/3/1623:49
 */
public class _剑指_Offer28_对称的二叉树 {
    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode();
        TreeNode left = new TreeNode(2,new TreeNode(3),new TreeNode(4));
        TreeNode right = new TreeNode(2,new TreeNode(4),new TreeNode(3));
        treeNode.left = left;
        treeNode.right = right;
        System.out.println(isSymmetric(treeNode));
    }
    public static boolean isSymmetric(TreeNode root) {
        if (root ==null )return true;
        return helper(root.left,root.right);
    }

    public static boolean helper(TreeNode left,TreeNode right){
        if (left == null && right==null) return true;
        if (right==null || left == null  || left.val != right.val) return false;
        return helper(left.left,right.right) && helper(left.right,right.left);
    }
}
